package cn.newtest.common.dbf;

/**
 * <p>
 * Title: RiseReport
 * </p>
 * <p>
 * Description: RiseReport
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: SZRiseSoft
 * </p>
 * 
 * @author WangChui(baiyibing@gmail.com)
 * @version 2.0 TODO
 * 
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Formater3 implements IFormater {
	private static final Log log = LogFactory.getLog(Formater3.class);

	public Formater3(int specialOP) {
		this.specialOP = specialOP;
	}

	private int specialOP = 3;

	public ArrayList operate(StringBuffer str, int len, String rowSpliter,
			String spliter, JDBField[] jdbFields) {
		log.debug("specialOP = 3");
		ArrayList alist = new ArrayList();
		String[] rows = StringUtils.split(str.toString(), rowSpliter);
		String[] data = null;
		Object[] tmp = null;
		for (int i = 0; i < rows.length; i++) {
			tmp = new Object[len];
			/*
			 * if(rows[i].endsWith(spliter)){ rows[i] = rows[i]+" "; }
			 */
			data = StringUtils.split(rows[i], spliter);
			// if (specialOP == 3) {// 导出的过滤就写在这里了，没有办法啊
			// data[0] = data[0].substring(4);
			//int rowCode = Integer.parseInt(data[2]);
			int rowCode = Integer.parseInt(data[3]);
			if (110000 <= rowCode && rowCode <= 113000) {// V208
				if(NumberUtils.isNumber(data[4])&& Float.parseFloat(data[4])==0){
						continue;// 费用为0不导出
				}					
			} else if (120000 <= rowCode && rowCode <= 129999) {// V207
				if(NumberUtils.isNumber(data[4])&& Float.parseFloat(data[4])==0){
					continue;// 费用为0不导出
				}
			} else if (130000 <= rowCode && rowCode <= 140000) {// V209
				if(NumberUtils.isNumber(data[4])&& Float.parseFloat(data[4])==0){
					continue;// 费用为0不导出，小分项不能填数字，保存进去是0，所以也不会导出
				}
			} else if (300000 <= rowCode && rowCode <= 304000) {// V211,可以放宽
				if(rowCode==303000){
					if (NumberUtils.isNumber(data[5])&& Float.parseFloat(data[5])==0 && NumberUtils.isNumber(data[6])&& Float.parseFloat(data[6])==0){
						continue;// 303000行23列为0不导出
					}
				}else{
					if(NumberUtils.isNumber(data[4])&& Float.parseFloat(data[4])==0){
						continue;// 其他行费用为0不导出
					}
				}
				if (rowCode == 300000)
					continue;// 总计不导出
			}
			// }
			// if (specialOP == 0 || specialOP == 3) {
			for (int j = 0; j < len; j++) {
				// (c != 'C' && c != 'N' && c != 'L' && c != 'D' && c != 'F'
				// && c !='B')
				//byb commnet
				//specialOP = 3非常特殊
				//导出确省都是带这项目代码或者单位代码的，现在的导出不要项目编码而是要求导出企业工程编码
				//所以把data[j+1]全部加1了，把排在首位的项目代码给略过去了
				//就是这样处理了
			 
				log.debug("jdbFields[" + j + "].getType()="
						+ jdbFields[j].getType());
				log.debug("data[" + j + "]=" + data[j+1]);
				if (jdbFields[j].getType() == 'N') {
					if (data[j+1].trim().equals(""))
						tmp[j] = new Integer("0");
					else
						tmp[j] = new Integer(data[j+1]);
				} else if (jdbFields[j].getType() == 'F') {
					if (data[j+1].trim().equals(""))
						tmp[j] = new Float("0");
					else
						tmp[j] = new Float(data[j+1]);
				} else if (jdbFields[j].getType() == 'B') {
					if (data[j+1].trim().equals(""))
						tmp[j] = new Double("0");
					else
						tmp[j] = new Double(data[j+1]);
				} else if (jdbFields[j].getType() == 'C') {
					int fieldLen = jdbFields[j].getLength();
					// if(fieldLen - 1 < data[j].length()){
					// tmp[j] = data[j].substring(0,fieldLen - 1);
					// 不明白李开始为什么要-1长度
					if (fieldLen < data[j+1].length()) {
						tmp[j] = data[j+1].substring(0, fieldLen);
					} else {
						tmp[j] = data[j+1];
					}
				} else if (jdbFields[j].getType() == 'D') {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
					try {
						tmp[j] = sdf.parse(data[j+1]);// 跨过名称代码
					} catch (Exception ex) {
						ex.printStackTrace();
						tmp[j] = new Date();
					}
				} else if (jdbFields[j].getType() == 'L') {
					tmp[j] = new Boolean(data[j+1]);// 跨过名称代码
				} else {
					tmp[j] = data[j+1];// 跨过名称代码
				}

			}

			alist.add(tmp);
		}
		return alist;

	}

}
